﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.DataEntities;
using Kingdee.K3.FIN.App.Core.ARAP.AgingAnalysisReport;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    [Description("二开应收款账龄分析表  增加自定义字段销售员")]
    [HotUpdate]
    public class AgingAnalysisEKService : ARAgingAnalysisService
    {
        private string CostCalBilltTableNames;

        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();
            CostCalBilltTableNames = dbService.CreateTemporaryTableName(this.Context);
            base.BuilderReportSqlAndTempTable(filter, CostCalBilltTableNames);
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            Boolean flag = Convert.ToBoolean(customFilter["ByBill"]);
            if (flag)
            {
                string F_DEV_SalerNumber_Id = customFilter["F_DEV_SalerNumber_Id"].ToString();//销售员ID
                if (!F_DEV_SalerNumber_Id.Equals("0"))
                {
                    string sql = string.Format(@"
                                                    SELECT B.*, EML.FNAME AS F_DEV_SalerName,salb.FNAME AS F_DEV_SALEBODY    
                                                    INTO {0}
                                                    FROM {1} B  
                                                    LEFT JOIN (  
                                                    SELECT FBILLNO, F_DEV_Saler,F_RDLV_SALEBODY FROM T_AR_RECEIVABLE 
                                                    UNION ALL  
                                                    SELECT FBILLNO, F_DEV_Saler,F_QLTK_saler as F_RDLV_SALEBODY FROM T_AR_RECEIVEBILL 
                                                    UNION ALL  
                                                    SELECT FBILLNO, F_DEV_Saler,F_QLTK_saler as F_RDLV_SALEBODY FROM T_AR_REFUNDBILL
                                                    ) A ON B.FBILLNO = A.FBILLNO  AND A.F_DEV_SALER='{2}'
                                                    LEFT JOIN  RDLV_t_sale_body_L salb on salb.fid = a.F_RDLV_SALEBODY
                                                    INNER JOIN T_HR_EMPINFO EM ON A.F_DEV_Saler = EM.FID  
                                                    INNER JOIN T_HR_EMPINFO_L EML ON EM.FID = EML.FID AND EML.FLOCALEID = '2052'  ", tableName, CostCalBilltTableNames, F_DEV_SalerNumber_Id);
                    DBUtils.Execute(this.Context, sql.ToString());
                }
                else
                {
                    string sql = string.Format(@"SELECT B.*, EML.FNAME AS F_DEV_SalerName,salb.FNAME AS F_DEV_SALEBODY  
                                                    INTO {0}
                                                    FROM {1} B  
                                                    LEFT JOIN (  
                                                        SELECT FBILLNO, F_DEV_Saler,F_RDLV_SALEBODY FROM T_AR_RECEIVABLE 
                                                        UNION ALL  
                                                        SELECT FBILLNO, F_DEV_Saler,F_QLTK_saler as F_RDLV_SALEBODY FROM T_AR_RECEIVEBILL 
                                                        UNION ALL  
                                                        SELECT FBILLNO, F_DEV_Saler,F_QLTK_saler as F_RDLV_SALEBODY FROM T_AR_REFUNDBILL
                                                    ) A ON B.FBILLNO = A.FBILLNO
                                                    LEFT JOIN  RDLV_t_sale_body_L salb on salb.fid = a.F_RDLV_SALEBODY
                                                    LEFT JOIN T_HR_EMPINFO EM ON A.F_DEV_Saler = EM.FID  
                                                    LEFT JOIN T_HR_EMPINFO_L EML ON EM.FID = EML.FID AND EML.FLOCALEID = '2052'  ", tableName, CostCalBilltTableNames);
                    DBUtils.Execute(this.Context, sql.ToString());
                }
            }
            else if (Convert.ToString(customFilter["ContactUnitType"]).Equals("BD_Customer"))
            {
                string sql = string.Format(@"select D.FNAME AS F_QLTK_BaseProperty_tzk,C.FNUMBER AS F_QLTK_BaseProperty_re5,F.FNAME AS F_QLTK_BaseProperty_ca9,A.* 
                                        INTO {0}
                                        from {1} A
                                        LEFT JOIN T_BD_CUSTOMER B ON A.FContactUnitNumber=B.FNUMBER
                                        LEFT JOIN T_HR_EMPINFO C ON B.F_RDLV_SELLER = C.FID
                                        LEFT JOIN T_HR_EMPINFO_L D ON C.FID = D.FID
                                        LEFT JOIN RDLV_t_OrderType E ON B.F_DEV_ORDERTYPE = E.FID
                                        LEFT JOIN RDLV_t_OrderType_L F ON E.FID=F.FID", tableName, CostCalBilltTableNames);
                DBUtils.Execute(this.Context, sql.ToString());
            }
            else 
            {
                string sql = string.Format(@"select * into {0} from {1}", tableName, CostCalBilltTableNames);
                DBUtils.Execute(this.Context, sql.ToString());
            }
        }
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            Boolean flag = Convert.ToBoolean(customFilter["ByBill"]);

            if (flag)
            {
                ReportHeader report = base.GetReportHeaders(filter);
                var F_DEV_SalerName = report.AddChild("F_DEV_SalerName", new Kingdee.BOS.LocaleValue("销售员", this.Context.UserLocale.LCID));
                var F_DEV_SALEBODY = report.AddChild("F_DEV_SALEBODY", new Kingdee.BOS.LocaleValue("销售主体", this.Context.UserLocale.LCID));
                F_DEV_SalerName.ColIndex = 14;
                F_DEV_SALEBODY.ColIndex = 15;
                return report;
            }
            else if (Convert.ToString(customFilter["ContactUnitType"]).Equals("BD_Customer"))
            {
                ReportHeader report = base.GetReportHeaders(filter);

                var F_QLTK_BaseProperty_tzk = report.AddChild("F_QLTK_BaseProperty_tzk", new Kingdee.BOS.LocaleValue("客户经理", this.Context.UserLocale.LCID));
                var F_QLTK_BaseProperty_re5 = report.AddChild("F_QLTK_BaseProperty_re5", new Kingdee.BOS.LocaleValue("客户经理编码", this.Context.UserLocale.LCID));
                var F_QLTK_BaseProperty_ca9 = report.AddChild("F_QLTK_BaseProperty_ca9", new Kingdee.BOS.LocaleValue("销售类型", this.Context.UserLocale.LCID));
                return report;
            }
            return base.GetReportHeaders(filter);
            //ReportHeader report = base.GetReportHeaders(filter);
            //report.AddChild("F_DEV_COUNT", new Kingdee.BOS.LocaleValue("再制品数量", this.Context.UserLocale.LCID), SqlStorageType.SqlDecimal, true).ColIndex=100;
            //return report;

        }
        public override void CloseReport()
        {
            if (string.IsNullOrWhiteSpace(CostCalBilltTableNames))
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();

            dbService.DeleteTemporaryTableName(this.Context, new string[1] { CostCalBilltTableNames });
            base.CloseReport();

        }
    }
}
